package com.edou.cms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.impl.cookie.DateUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.base.util.CommonUtil;
import com.edou.cms.dao.ICmsTagDao;
import com.edou.cms.dto.CmsTagCondition;
import com.edou.cms.entity.CmsArticle;
import com.edou.cms.entity.CmsTag;
import com.edou.cms.service.ICmsTagService;

@Service("cmsTagServiceImpl")
@Transactional(value = "transactionManager")
public class CmsTagServiceImpl implements ICmsTagService{

	@Resource(name = "cmsTagDaoImpl")
	private ICmsTagDao cmsTagDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listCmsTagByPage(PageResult<CmsTag> pageResult,CmsTagCondition condition){
		int rows = cmsTagDao.listCmsTagByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<CmsTag> list = cmsTagDao.listCmsTagByPage(rowBounds,condition);
		pageResult.setResult(list);
	}
	
	/**
	 * 根据条件查询标签
	 * @return
	 */
	public List<CmsTag> listCmsTagByCondition(CmsTagCondition condition) {
		List<CmsTag> list = cmsTagDao.listCmsTagByCondition(condition);
		for (CmsTag cmsTag : list) {
			List<CmsArticle> cmsArticles = cmsTag.getCmsArticles();
			Date now = new Date();
			for (CmsArticle cmsArticle : cmsArticles) {
				cmsArticle.setDateTips(CommonUtil.getDistanceTime(now, cmsArticle.getCreateDate()));
				if(cmsArticle.getCreateDate() != null){
					//判断是否显示最新标签
					int formatDate = Integer.parseInt(DateUtils.formatDate(cmsArticle.getUpdateDate(),"yyyyMMdd"));
					int nowDate = Integer.parseInt(DateUtils.formatDate(now,"yyyyMMdd"));
					if(nowDate-formatDate <= 3){
						cmsArticle.setShowNew(1);
					}else{
						cmsArticle.setShowNew(0);
					}
				}else{
					cmsArticle.setShowNew(0);
				}
			}
		}
		return list;
	}
	
	/**
	 * 根据专栏id查询标签
	 * @param columnId
	 * @return
	 */
	public List<CmsTag> listCmsTagByColumnId(String columnId) {
		return cmsTagDao.listCmsTagByColumnId(columnId);
	}
	
	/**
	 * 热门标签
	 */
	public List<CmsTag> listCmsTagHotsList() {
		return cmsTagDao.listCmsTagHotsList();
	}
	
	/**
	 * 根据栏目id查询文章热门标签
	 */
	public List<CmsTag> listCmsTagHotsListByChannelId(String channelId) {
		return cmsTagDao.listCmsTagHotsListByChannelId(channelId);
	}

	
	/**
	 * 查询所有标签
	 * @return
	 */
	public List<CmsTag> listCmsTagAll() {
		return cmsTagDao.listCmsTagAll();
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 文章标签
	 */
	public CmsTag getCmsTagById(String id){
		return cmsTagDao.getCmsTagById(id);
	}

	/**
	 * 新增
	 * @param item 文章标签
	 */
	public void add(CmsTag item){
		cmsTagDao.add(item);
	}

	/**
	 * 修改
	 * @param item 文章标签
	 */
	public void update(CmsTag item){
		CmsTag cmsTag = cmsTagDao.getCmsTagById(item.getId());
		cmsTag.setSort(item.getSort());
		cmsTag.setType(item.getType());
		cmsTag.setName(item.getName());
		cmsTagDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		cmsTagDao.delByIds(ids);
	}

	/**
	 * 根据类别查询标签
	 */
	public List<CmsTag> listCmsTagByType(CmsTagCondition condition) {
		return cmsTagDao.listCmsTagByType(condition);
	}

	/**
	 * 根据排序方式，进行查询热门标签
	 */
	public List<CmsTag> listCmsTagHotsBySort(Integer limit) {
		return cmsTagDao.listCmsTagHotsBySort(limit);
	}
}

